//
//  LBWNewfeatureViewController.swift
//  weibo
//
//  Created by Limonwei on 15/12/8.
//  Copyright © 2015年 heima. All rights reserved.
//
//新特性视图

import UIKit

class LBWNewfeatureViewController: UIViewController, UIScrollViewDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        //更新UI
        setupUI()
    }
    
    //初始化视图
    private func setupUI(){
        
        //1.添加scrollView 大小等于当前视图的大小
        scrollView.frame = view.bounds
        
        view.addSubview(scrollView)
        
        //添加页面控制器
        pageControl.frame = CGRect(x: scrollView.frame.width * 0.5, y: scrollView.frame.height - 100, width: 0, height: 0)
        
        view.addSubview(pageControl)

        //2.往scrollView里添加4张图片
        let count = 4
        
        for i in 0...count{
            
            //初始化imageView
            let imageView = UIImageView(image: UIImage(named: "new_feature_\(i + 1)"))
            
            imageView.frame = CGRect(x: CGFloat(i) * scrollView.frame.width, y: 0, width: scrollView.frame.width, height: scrollView.frame.height)
            
            scrollView.addSubview(imageView)
            
            //判断如果是最后一页,设置最后一页内容
            if i == count - 1 {
                setupLastPage(imageView)
                
            }
            
        }
        
        
        //3.设置滚动范围 大小
        
        scrollView.contentSize = CGSize(width: CGFloat(count) * scrollView.frame.width, height: 0)
        
        
        //4.滚动页数
        pageControl.numberOfPages = count
    }
    
    
    //设置最后一页的内容  添加两个按钮 进入
    private func setupLastPage(lastPage: UIView) {
        
        //图片开启用户交互
        lastPage.userInteractionEnabled = true
        
        enterButton.center = CGPoint(x: lastPage.frame.width * 0.5, y: lastPage.frame.height - 150)
        
        shareButton.center = CGPoint(x: enterButton.center.x, y: CGRectGetMinY(enterButton.frame) - 30)
        
        lastPage.addSubview(enterButton)
        lastPage.addSubview(shareButton)
    }
    
    
    
    //scrollView代理
    func scrollViewDidScroll(scrollView: UIScrollView) {
        //计算当前滑动到第几页 只有当滑动到页面的一半的时候才会改变页码
        let page = scrollView.contentOffset.x / scrollView.frame.width + 0.5
        
        //printLog(page)
        
        pageControl.currentPage = Int(page)
    }
    
    
    
    
    //懒加载控件
    //1.懒加载scrollView
    private lazy var scrollView: UIScrollView = {
        
        //定义scrollView
        let scrollView = UIScrollView()
        
        //设置代理
        scrollView.delegate = self
        
        //开启分页
        scrollView.pagingEnabled = true
        
        //关闭边缘的弹簧效果
        scrollView.bounces = false
        
        //隐藏滚动条
        scrollView.showsHorizontalScrollIndicator = false
        
        return scrollView
    }()
    
    //2.懒加载分页控件pageControl
    
    private lazy var pageControl: UIPageControl = {
        
        let pageControl = UIPageControl()
        
        //设置选中颜色
        pageControl.currentPageIndicatorTintColor = UIColor.orangeColor()
        
        //设置默认颜色
        pageControl.pageIndicatorTintColor = UIColor.grayColor()
        
        return pageControl
        
    }()
    
    //3.懒加载分享按钮
    private lazy var shareButton: UIButton = {
        
        let button = UIButton()
        //添加按钮点击事件 按钮点击有参数的时候后面有一个冒号:
        button.addTarget(self, action: "shareButtonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        
        //设置不同状态下的图片
        button.setImage(UIImage(named: "new_feature_share_true"), forState: UIControlState.Selected)
        
        button.setImage(UIImage(named: "new_feature_share_false"), forState: UIControlState.Normal)
        
        //设置按钮文字
        button.setTitle(" 分享到微博", forState: UIControlState.Normal)
        
        //设置文字颜色
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        
        //设置文字跟图片之间的间隔 或者在分享微博前面加个空格
        //button.titleEdgeInsets = UIEdgeInsets(top: 0, left:5 , bottom: 0, right: -5)
        
        button.sizeToFit()
        
        
        return button
    }()

    
    
    //4.懒加载进入按钮
    
    private lazy var enterButton : UIButton = {
        
        let enterButton = UIButton()
        
        //按钮点击没有参数传递的时候没有冒号
        enterButton.addTarget(self, action: "enterButtonClick", forControlEvents: .TouchUpInside)
        
        enterButton.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: .Highlighted)
        enterButton.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: .Normal)
        
        enterButton.setTitle("进入微博", forState: .Normal)
        
        enterButton.sizeToFit()

        return enterButton
    }()

 
    //按钮的点击事件
    
    @objc private func shareButtonClick(button: UIButton){
        button.selected = !button.selected
    }
    
    
    @objc private func enterButtonClick(){
        
        printLog("点击进入微博")
        
        NSNotificationCenter.defaultCenter().postNotificationName(LBWSwitchRootVCNotifation, object: nil)
        
    }

  

}






































